package com.bosicen.crm.dao.sysmanage;

import java.util.List;


import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import com.bosicen.crm.dao.HibernateDao;
import com.bosicen.crm.dao.entity.Product;
import com.bosicen.crm.tools.Page;
/**
 * 商品管理
 * @author tigerlei
 * 
 *
 */
@Component("productDao")
@SuppressWarnings("unchecked")
public class ProductDao extends HibernateDao<Product, Integer> {

	/****
	 * 查询列表
	 * @param id
	 * @return
	 */
	
	public List<Product> findProductList(Product product){
		StringBuilder sb=new StringBuilder();
		sb.append(" from Product where companyid=?  ");
		if(product.getType()>0) 
			sb.append(" and type=").append(product.getType());
		sb.append(" order by id asc");
		return this.getSession().createQuery(sb.toString()).setParameter(0, product.getCompanyid()).list();
	}
	
	/**
	 * 按ID查询
	 * @param id
	 * @return
	 */
	public Product findProductById(int id){
		return this.get(id);
	}
	
	/***
	 * 删除
	 * @param c
	 */
	public void delProduct(Product c){
		this.delete(c.getId());
	}
	
	/**
	 * 保存
	 */
	public void saveOrUpdate(Product product){
		if(product.getId()>0){
			Product pd = this.get(product.getId());
			pd.setOpenmethod(pd.getOpenmethod());
			product.setImage(pd.getImage());
		}
		this.getSession().merge(product);
	}
	
	/**
	 * 带条件按分页查询
	 * @param page
	 * @param product
	 * @return
	 */
	public Page findProduct4Page(Page page, Product product) {
		String sql = " from Product where companyid=?  ";
		if (product.getType() > 0)
			sql += " and type=" + product.getType();
		if(StringUtils.isNotBlank(product.getProductname())){
			sql += " and productname like'%"+product.getProductname()+"%' ";
		}
		if(StringUtils.isNotBlank(product.getProductno())){
			sql += " and productno='"+product.getProductno()+"' ";
		}
		sql += " order by id asc";
		return this.findPage(page, sql, product.getCompanyid());
	}
}
